<?php

namespace Ticket\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Ticket\Entity\Ticket;

class ReportController extends AbstractActionController {

    protected $entityManager;
    protected $clientRepository;

    public function findAction() {
        $clientcriteria = new \Client\Entity\ClientCriteria();
        $clients = array();

        $form = $this->getClientCriteriaForm();
        $form->setAttribute('action', $this->url()->fromRoute('reportticket'));
        $form->bind($clientcriteria);

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $clients = $this->getClientRepository()->findByCriteria($clientcriteria);
            }
        }
        return array('form' => $form, 'clients' => $clients, 'post' => $request->isPost());
    }

    public function historyAction() {
        $client = $this->getClientRepository()->find((int) $this->params()->fromRoute('id', 0));
        $ticketRepository = $this->getServiceLocator()->get('Ticket\Repository\TicketRepository');
        $tickets = $ticketRepository->findByClient($client);
        return array('tickets' => $tickets, 'client' => $client);
    }

    public function newTicketAction() {
        $id = (int) $this->params()->fromRoute('id', 0);
        $ticket = new Ticket();
        $client = $this->getClientRepository()->find($id);
        $ticket->setClient($client);
        $form = $this->getTicketForm();
        $form->setAttribute('action', $this->url()->fromRoute('reportticket', array('action' => 'newTicket', 'id' => $id)));
        $form->get('submit')->setValue('Add');
        $form->bind($ticket);

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $ticket->setTime(new \DateTime());
                $ticket->setUser($this->getAuthenticationService()->getIdentity());
                $this->getEntityManager()->persist($ticket);
                $this->getEntityManager()->flush();
                return $this->redirect()->toRoute('ticket');
            }
        }
        return array('form' => $form);
    }

    /**
     * @return \Doctrine\ORM\EntityManager
     */
    public function getEntityManager() {
        if (!$this->entityManager) {
            $this->entityManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }

        return $this->entityManager;
    }

    /**
     * @return \Client\Repository\ClientRepository
     */
    public function getClientRepository() {
        if (!$this->clientRepository) {
            $this->clientRepository = $this->getServiceLocator()->get('Client\Repository\ClientRepository');
        }

        return $this->clientRepository;
    }

    public function getClientCriteriaForm() {
        return $this->getServiceLocator()->get('Client\Form\ClientCriteriaForm');
    }

    public function getTicketForm() {
        return $this->getServiceLocator()->get('Ticket\Form\TicketForm');
    }

    public function getAuthenticationService() {
        return $this->getServiceLocator()->get('Authentication\Service\AuthenticationService');
    }

}